import os

def generate_core_md(md_path, images, texts, tables, layout_path, godot_advice=False, slide_width=None, slide_height=None):
    def shadow_mark(meta):
        shadow = meta.get('shadow')
        if shadow and shadow.get('visible'):
            return '（含阴影）'
        return ''
    def layout_shadow_names(elements, prefix=''):
        names = []
        for elem in elements:
            if isinstance(elem, dict):
                mark = shadow_mark(elem)
                names.append(f"{prefix}{elem.get('name', '')}{mark}")
                if 'children' in elem and isinstance(elem['children'], list):
                    names.extend(layout_shadow_names(elem['children'], prefix+'  '))
        return names
    with open(md_path, 'w', encoding='utf-8') as f:
        f.write('# 核心说明.md\n')
        if slide_width and slide_height:
            f.write(f'## 页面大小\n- 宽度: {slide_width} (EMU)\n- 高度: {slide_height} (EMU)\n')
        f.write('## 图片资源\n')
        for img in images:
            meta = img['meta']
            mark = shadow_mark(meta)
            # 优先使用image_path字段
            img_path = meta.get('image_path') or os.path.relpath(img['path'], os.path.dirname(md_path))
            f.write(f"- {img['name']}{mark} → {img_path}\n")
            f.write(f"  - 尺寸: {meta['width']}x{meta['height']}\n")
            f.write(f"  - 位置: (x={meta['left']}, y={meta['top']})\n")
            if meta.get('crop'):
                f.write(f"  - 裁剪: {meta['crop']}\n")
        f.write('\n## 文本资源\n')
        for txt in texts:
            meta = txt['meta']
            mark = shadow_mark(meta)
            f.write(f"- {txt['name']}{mark} → {os.path.relpath(txt['path'], os.path.dirname(md_path))}\n")
            if meta['runs']:
                f.write(f"  - 内容: {''.join([r['text'] for r in meta['runs']])}\n")
                f.write(f"  - 字体: {meta['runs'][0]['font']}, {meta['runs'][0]['size']}pt\n")
        f.write('\n## 表格资源\n')
        for tbl in tables:
            meta = tbl['meta']
            mark = shadow_mark(meta)
            f.write(f"- {tbl['name']}{mark} → {os.path.relpath(tbl['path'], os.path.dirname(md_path))}\n")
            f.write(f"  - 行数: {meta['row_count']}，列数: {meta['col_count']}\n")
        f.write(f"\n## 布局数据\n- {os.path.relpath(layout_path, os.path.dirname(md_path))}\n")
        # 递归输出布局中所有元素及阴影标记
        try:
            import json
            with open(layout_path, 'r', encoding='utf-8') as lf:
                layout_json = json.load(lf)
            f.write('\n### 布局元素及阴影标记\n')
            names = layout_shadow_names(layout_json.get('elements', []))
            for n in names:
                f.write(f'- {n}\n')
        except Exception as e:
            f.write(f'布局解析异常: {e}\n')
        if godot_advice:
            f.write('\n## Godot适配建议\n')
            f.write('1. 坐标转换：PPT的Y轴坐标需用 `Godot_Y = 视口高度 - PPT_Y` 翻转\n')
            f.write('2. 图片导入：建议启用 `Filter=false` 保持像素锐利\n')
            f.write('3. 字体匹配：优先使用 `Theme` 统一管理文本样式\n') 